package com.novatec.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

public class VendedoresDAO {
	
	private final Logger logger = Logger.getLogger(this.getClass());
	private SimpleJdbcTemplate simpleJdbcTemplate;
//	private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyy");

	@Autowired
    public void init(DataSource dataSource) {
        this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
    }
	

	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public List<Map<String, Object>> getVendedoresLikeNick(String nick){
		
		String sql= "SELECT u.idUsuario as idVendedor, u.login as vendedor FROM vendedor v, usuario u WHERE v.idUsuario = u.idUsuario and indActivo= '1' and u.login like '" + nick + "%'";
		logger.debug(sql);
		try{
			return simpleJdbcTemplate.queryForList(sql);
		}catch(Exception e){
			e.printStackTrace();
			return new ArrayList<Map<String,Object>>();
		}
	}

	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	
	public List<Map<String, Object>> getVendedoresAll(){
		
		String sql= "SELECT u.idUsuario as idVendedor, u.login as vendedor FROM vendedor v, usuario u WHERE v.idUsuario = u.idUsuario and indActivo= '1'";
		logger.debug(sql);
		try{
			return simpleJdbcTemplate.queryForList(sql);
		}catch(Exception e){
			e.printStackTrace();
			return new ArrayList<Map<String,Object>>();
		}
	}	
}
